//
//  UserCenter.swift
//  Center
//
//  Created by Flutter on 2021/8/3.
//

import Foundation
import Standard
import Center_Storage


public extension UserCenter {
    
    static var personID: String? = {
        do {
            guard let storage: PersonIDStorage = try store.sync.get() else { return nil }
            return storage.value
        } catch {
            logger.error("\(error)")
            return nil
        }
    }() {
        didSet {
            do {
                switch personID {
                case .none:
                    try store.sync.delete(key: PersonIDStorage.key)
                case .some(let personID):
                    try store.sync.put(storableObject: PersonIDStorage(value: personID))
                }
            } catch {
                logger.error("\(error)")
            }
        }
    }
    
    static func createDefaultPerson() {
        
        let once = UserCenter.once
        if once.createDefaultPerson == false {
            let p = Person()
            p.name = "默认"
            p.isDefault = true
            let result = p.insert()
            if result == false {
                logger.error("默认 Member 创建失败")
            }
            once.createDefaultPerson = true
            UserCenter.once = once
        }
        
        
    }
}





